package cz.matasek.dbLogger.gui;

import cz.matasek.dbLogger.Label;
import cz.matasek.dbLogger.model.Database;
import cz.matasek.dbLogger.sql.SQLStatement;

/**
 * This panel contains basic information about the database and allows creating
 * the logging environment.
 *
 * @author Luboš Matásek
 */
public class DatabaseInfoPanel extends javax.swing.JPanel implements Refreshable {

    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = -8077037794257033984L;
    /**
     * Database object.
     */
    private Database database;

    /**
     * Creates a new <code>DatabaseInfoPanel</code>.
     *
     * @param database the database object
     */
    public DatabaseInfoPanel(Database database) {
        initComponents();

        this.database = database;

        refresh();
    }

    @Override
    public void refresh() {
        // sets the database description
        _databaseDescription.setText(
                database.getConnection().getDatabaseDescription());

        // sets logging objects table model
        _loggingObjects.setModel(new LoggingObjectsTableModel(
                database.getConnection().getLoggingObjects()));

        // sets label to logging environment and logging table buttons
        if (database.isLoggingAvailable()) {
            _loggingEnvironment.setText(Label.DROP);
            _loggingTable.setEnabled(true);
        } else {
            _loggingEnvironment.setText(Label.CREATE);
            _loggingTable.setEnabled(false);
        }

        if (database.hasLoggingTable()) {
            _loggingTable.setText(Label.DROP);
        } else {
            _loggingTable.setText(Label.CREATE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        _databaseDescription = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        _disconnect = new javax.swing.JButton();
        _loggingTable = new javax.swing.JButton();
        _loggingEnvironment = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        _loggingObjects = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Connected to");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Logging environment");

        jScrollPane1.setBorder(null);

        _databaseDescription.setBackground(getBackground());
        _databaseDescription.setColumns(20);
        _databaseDescription.setEditable(false);
        _databaseDescription.setLineWrap(true);
        _databaseDescription.setRows(3);
        _databaseDescription.setWrapStyleWord(true);
        _databaseDescription.setBorder(null);
        jScrollPane1.setViewportView(_databaseDescription);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Logging table");

        _disconnect.setText("Disconnect");
        _disconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _disconnectActionPerformed(evt);
            }
        });

        _loggingTable.setText("Create");
        _loggingTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _loggingTableActionPerformed(evt);
            }
        });

        _loggingEnvironment.setText("Create");
        _loggingEnvironment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _loggingEnvironmentActionPerformed(evt);
            }
        });

        _loggingObjects.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(_loggingObjects);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Logging  objects");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(_disconnect, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(37, 37, 37)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(_loggingEnvironment, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(233, 233, 233)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(_loggingTable, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 213, Short.MAX_VALUE)))
                        .addContainerGap(84, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap(570, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(598, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(_disconnect))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_loggingEnvironment)
                    .addComponent(_loggingTable))
                .addGap(34, 34, 34)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Disconects from the database.
     *
     * @param evt action event
     */
    private void _disconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__disconnectActionPerformed
        // disconnects from the database
        database.getConnection().disconnect();

        // clears the connection
        SQLStatement.clearStatementsCache();

        // refresh GUI
        Gui gui = Gui.getInstance();
        gui.setDisconnected();

        gui.repaint();
}//GEN-LAST:event__disconnectActionPerformed

    /**
     * Creates or drops the logging table.
     *
     * @param evt action event
     */
    private void _loggingTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__loggingTableActionPerformed
        if (database.hasLoggingTable()) {
            if (!Dialog.dropLoggingTable()) {
                return;
            }

            // drops logging table
            database.getConnection().dropLoggingTable();

            // refreshes all
            Gui.getInstance().refresh();
        } else {
            // creates logging table
            database.getConnection().createLoggingTable();

            // refreshes all
            Gui.getInstance().refresh();
        }
}//GEN-LAST:event__loggingTableActionPerformed

    /**
     * Creates or drops the logging environment.
     *
     * @param evt action event
     */
    private void _loggingEnvironmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__loggingEnvironmentActionPerformed
        if (database.isLoggingAvailable()) {
            if (!Dialog.dropLoggingEnvironment()) {
                return;
            }

            database.getConnection().dropLoggingEnvironment(true);

            // refreshes all
            Gui.getInstance().refresh();
        } else {
            // gets the prefix
            String prefix = Dialog.loggingPrefix();

            // input cancelled
            if (prefix == null) {
                return;
            }

            // input too short
            if (prefix.length() == 0) {
                Dialog.loggingPrefix();
                return;
            }


            database.getConnection().createLoggingEnvironment(prefix);

            // refreshes all
            Gui.getInstance().refresh();
        }
}//GEN-LAST:event__loggingEnvironmentActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea _databaseDescription;
    private javax.swing.JButton _disconnect;
    private javax.swing.JButton _loggingEnvironment;
    private javax.swing.JTable _loggingObjects;
    private javax.swing.JButton _loggingTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
